﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using QRCoder;
using _180FrameWork.Common.SM;


namespace _180FrameWork.Common
{
    public static class CodeHelper
    {
        public static string md5(string str, int code = 16)
        {
            if (code == 16) //16位MD5加密（取32位加密的9~25字符） 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }
            else//32位加密 
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }
        }

        public static string md5(byte[] bytes)
        {
            MD5 m = new MD5CryptoServiceProvider();
            var returnBytes = m.ComputeHash(bytes);
            string bytesToString = null;
            for (int i = 0; i < returnBytes.Length; i++)
            {
                bytesToString += returnBytes[i].ToString("x2");
            }

            //  string bytesToString = System.Text.Encoding.ASCII.GetString(returnBytes);
            return bytesToString;
        }

        private static string SKey = "cnpl1234";

        /// <summary>
        /// 进行DES加密。
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <param name="sKey">密钥，且必须为8位。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = Encoding.UTF8.GetBytes(sKey);
                des.IV = Encoding.UTF8.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());

                ms.Close();
                return str;
            }
        }

        /// <summary>
        /// 进行DES解密。
        /// </summary>
        /// <param name="pToDecrypt">要解密的以Base64</param>
        /// <param name="sKey">密钥，且必须为8位。</param>
        /// <returns>已解密的字符串。</returns>
        public static string Decrypt(string pToDecrypt, string sKey)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                des.Key = Encoding.UTF8.GetBytes(sKey);
                des.IV = Encoding.UTF8.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="pToEncrypt"></param>
        /// <returns></returns>
        public static string Encrypt(string pToEncrypt)
        {
            return Encrypt(pToEncrypt, SKey);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <returns></returns>
        public static string Decrypt(string pToDecrypt)
        {
            return Decrypt(pToDecrypt, SKey);
        }

        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static MemoryStream CreateQrCodeStream(string str)
        {
            var qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(str, QRCodeGenerator.ECCLevel.Q);
            BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);
            byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(10);
            return new MemoryStream(qrCodeAsBitmapByteArr);
        }
        /// <summary>
        /// 创建条码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="BarcodeType"></param>
        /// <returns></returns>
        public static MemoryStream CreateBarCodeStream(string code, int height = 100, int width = 250, string BarcodeType = "CODE128")
        {

            BarcodeLib.Barcode b = new BarcodeLib.Barcode();
            b.BackColor = System.Drawing.Color.White;
            b.ForeColor = System.Drawing.Color.Black;
            b.IncludeLabel = true;
            b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
            b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
            b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);
            b.LabelFont = font;

            b.Height = height;
            b.Width = width;
            BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;
            switch (BarcodeType)
            {
                case "CODE128":
                    type = BarcodeLib.TYPE.CODE128;
                    break;
                case "CODE39":
                    type = BarcodeLib.TYPE.CODE39;
                    break;
                case "CODE39Extended":
                    type = BarcodeLib.TYPE.CODE39Extended;
                    break;
                case "CODE128A":
                    type = BarcodeLib.TYPE.CODE128A;
                    break;
                case "CODE128B":
                    type = BarcodeLib.TYPE.CODE128B;
                    break;
                case "CODE128C":
                    type = BarcodeLib.TYPE.CODE128C;
                    break;
                case "CODE93":
                    type = BarcodeLib.TYPE.CODE93;
                    break;
            }
            Image image = b.Encode(type, code);
            byte[] buffer = b.GetImageData(BarcodeLib.SaveTypes.JPG);
            return new MemoryStream(buffer);
        }
        /// <summary>
        /// base64加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Base64Encode(string value)
        {
            byte[] inputByteArray = Encoding.UTF8.GetBytes(value);
            return Convert.ToBase64String(inputByteArray);
        }
        /// <summary>
        /// base64解密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Base64Decode(string value)
        {
            byte[] inputByteArray = Convert.FromBase64String(value);
            return Encoding.UTF8.GetString(inputByteArray);
        }
    }
}
